package org.szrcb.temu.SalesManage.dao;

import org.apache.ibatis.annotations.*;
import org.szrcb.temu.SalesManage.DTO.AfterSalesDTO;
import org.szrcb.temu.SalesManage.pojo.Warehouse;

import java.util.List;

@Mapper
public interface WarehouseDao {

    @Select("SELECT count(1) from warehouse_info WHERE mall_id = #{mallId} AND sku_ext_code = #{skuExtCode}")
    int checkSkuCodeExist(@Param("mallId") String mallId, @Param("skuExtCode") String skuExtCode);

    @Update("UPDATE warehouse_info SET " +
            "product_name = #{warehouse.productName}," +
            "class_name = #{warehouse.className}," +
            "warehouse_inventory_num = #{warehouse.warehouseInventoryNum}," +
            "unavailable_warehouse_inventory_num = #{warehouse.unavailableWarehouseInventoryNum}," +
            "wait_receive_num = #{warehouse.waitReceiveNum}," +
            "product_skc_id = #{warehouse.productSkcId}," +
            "product_sku_id = #{warehouse.productSkuId}," +
            "currency_type = #{warehouse.currencyType}," +
            "supplier_price = #{warehouse.supplierPrice}," +
            "product_skc_picture = #{warehouse.productSkcPicture} ," +
            "today_sale_volume = #{warehouse.todaySaleVolume} ," +
            "last_seven_days_sale_volume = #{warehouse.lastSevenDaysSaleVolume} ," +
            "last_thirty_days_sale_volume = #{warehouse.lastThirtyDaysSaleVolume} " +
            "WHERE mall_id=#{warehouse.mallId} AND sku_ext_code = #{warehouse.skuExtCode}")
    void updateWarehouse(@Param("warehouse") Warehouse warehouse);

    @Insert("INSERT INTO warehouse_info (" +
            "mall_id," +
            "sku_ext_code," +
            "product_name," +
            "class_name," +
            "warehouse_inventory_num," +
            "unavailable_warehouse_inventory_num," +
            "wait_receive_num," +
            "product_skc_id," +
            "product_sku_id," +
            "currency_type," +
            "supplier_price," +
            "buying_price," +
            "product_skc_picture," +
            "today_sale_volume," +
            "last_seven_days_sale_volume," +
            "last_thirty_days_sale_volume," +
            "is_deleted) " +
            "VALUES (" +
            "#{warehouse.mallId}, " +
            "#{warehouse.skuExtCode}, " +
            "#{warehouse.productName}, " +
            "#{warehouse.className}, " +
            "#{warehouse.warehouseInventoryNum}, " +
            "#{warehouse.unavailableWarehouseInventoryNum}, " +
            "#{warehouse.waitReceiveNum}, " +
            "#{warehouse.productSkcId}, " +
            "#{warehouse.productSkuId}, " +
            "#{warehouse.currencyType}, " +
            "#{warehouse.supplierPrice}, " +
            "#{warehouse.buyingPrice}, " +
            "#{warehouse.productSkcPicture}, " +
            "#{warehouse.todaySaleVolume}, " +
            "#{warehouse.lastSevenDaysSaleVolume}, " +
            "#{warehouse.lastThirtyDaysSaleVolume}, " +
            "0)" +
            ";")
    void saveWarehouse(@Param("warehouse") Warehouse warehouse);

    @Select("<script>SELECT * FROM warehouse_info WHERE mall_id=#{mallId} " +
            "<if test = \" productName != null and  productName != '' \"> " +
            "AND product_name like '%${productName}%'" +
            "</if>" +
            "<if test = \" skuExtCode != null and  skuExtCode != '' \"> " +
            "AND sku_ext_code like '%${skuExtCode}%'" +
            "</if>" +
            "<if test = \" productSkuId != null and  productSkuId != '' \"> " +
            "AND product_sku_id like '%${productSkuId}%'" +
            "</if>" +
            "</script>")
    List<Warehouse> queryWarehouse(@Param("mallId") String mallId, @Param("productName") String productName, @Param("skuExtCode") String skuExtCode, @Param("productSkuId") String productSkuId);


    @Update("UPDATE warehouse_info SET buying_price = #{buyingPrice} WHERE sku_ext_code = #{skuExtCode}")
    void updateBuyingPriceBySkuExtCode(String skuExtCode, String buyingPrice);

    @Select("SELECT product_sku_id from warehouse_info WHERE mall_id = #{mallId}")
    List<Long> queryProductSkuIds(@Param("mallId") String mallId);

    @Select("<script>SELECT product_sku_id,product_skc_picture,buying_price,supplier_price FROM warehouse_info" +
            " WHERE product_sku_id in"
            + "<foreach collection='productSkuIds' item='item' open='(' separator=',' close=')'> "
            + "#{item} "
            + "</foreach> " +
            "</script>")
    List<Warehouse>  queryMallListByProductId(@Param("productSkuIds") List<String> productSkuIds);
}
